
package com.pubinfo.passbook.common.config;

import cn.hutool.core.collection.CollectionUtil;
import com.fasterxml.classmate.TypeResolver;
import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.List;

@Configuration
@EnableSwagger2WebMvc
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfiguration {
  
  private final TypeResolver typeResolver;
  private final OpenApiExtensionResolver openApiExtensionResolver;
  
  @Autowired
  public SwaggerConfiguration(TypeResolver typeResolver, OpenApiExtensionResolver openApiExtensionResolver) {
    this.typeResolver = typeResolver;
    this.openApiExtensionResolver = openApiExtensionResolver;
  }
  
  private ApiKey apiKey() {
    return new ApiKey("BearerToken", "Authorization", "header");
  }
  
  private List<ApiKey> securitySchemes() {
    return CollectionUtil.newArrayList(new ApiKey("BearerToken", "Authorization", "header"));
  }
  
  @Bean(value = "defaultApi")
  public Docket defaultApi() {
    Docket docket = new Docket(DocumentationType.SWAGGER_2)
        .apiInfo(apiInfo())
        .groupName("1.默认接口")
        .select()
        .apis(RequestHandlerSelectors.any())
        //.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
        .paths(PathSelectors.any())
        .build()
        .extensions(openApiExtensionResolver.buildExtensions("1.默认接口"))
        .securityContexts(CollectionUtil.newArrayList(securityContext()));
    // .securitySchemes(securitySchemes());
    return docket;
  }
  
  private SecurityContext securityContext() {
    return SecurityContext.builder()
        .securityReferences(defaultAuth())
        .build();
  }
  
  List<SecurityReference> defaultAuth() {
    AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
    AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
    authorizationScopes[0] = authorizationScope;
    return CollectionUtil.newArrayList(new SecurityReference("BearerToken", authorizationScopes));
  }
  
  private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
        .description("# swagger-bootstrapRESTful APIs")
        .termsOfServiceUrl("http://www.xx.com/")
        .contact("2465915902@qq.com")
        .version("1.0")
        .build();
  }
  
}
